package com.fanwh.healthycampus2.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fanwh.healthycampus2.R;
import com.fanwh.healthycampus2.bean.HealthyInformation;
import com.fanwh.healthycampus2.bean.User;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

import static cn.bmob.v3.Bmob.getApplicationContext;

/**
 * A simple {@link Fragment} subclass.
 */
public class UploadFragment extends Fragment {

    private Button upload;
    private EditText name;
    private EditText studentID;
    private EditText school;
    private EditText phone;
    private EditText temperature;


    public UploadFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate =  inflater.inflate(R.layout.fragment_upload, container, false);
        upload = inflate.findViewById(R.id.sign_upload);
        name = inflate.findViewById(R.id.sign_name);
        studentID = inflate.findViewById(R.id.sign_studentID);
        school = inflate.findViewById(R.id.sign_school);
        phone = inflate.findViewById(R.id.sign_phone);
        temperature = inflate.findViewById(R.id.sign_temperature);

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String user_name = name.getText().toString();
                String user_studentID = studentID.getText().toString();
                String user_school = school.getText().toString();
                String user_phone = phone.getText().toString();
                String user_temperature = temperature.getText().toString();
                //非空验证
                if(user_name.isEmpty()||user_studentID.isEmpty()||user_school.isEmpty()||
                        user_phone.isEmpty()||user_temperature.isEmpty()){
                    Toast.makeText(getActivity(),"所填信息不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                //在HeathyInformation表中添加信息
                User user = BmobUser.getCurrentUser(User.class);
                HealthyInformation healthyInformation = new HealthyInformation();
                healthyInformation.setStudent(user);
                healthyInformation.setName(user_name);
                healthyInformation.setStudentID(user_studentID);
                healthyInformation.setSchool(user_school);
                healthyInformation.setPhoneNumber(user_phone);
                healthyInformation.setTemperature(user_temperature);
                healthyInformation.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if(e == null){
                            Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                            name.setText("");
                            studentID.setText("");
                            school.setText("");
                            phone.setText("");
                            temperature.setText("");
                        }else {
                            Log.i("Bmob","失败"+e.getMessage());
                        }
                    }
                });
            }
        });
        return inflate;
    }

}
